package com.haoshuang.zhouzhoubang.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityBackBondBinding
import com.haoshuang.zhouzhoubang.model.bean.ReturnBean
import com.haoshuang.zhouzhoubang.ui.dialog.BjDialog
import com.haoshuang.zhouzhoubang.ui.vm.BackBondViewModel2
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger


/**
 *
 */
class BackBondActivity2 : BaseVMActivity<BackBondViewModel2, ActivityBackBondBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.click { finish() }
        binding.appBar.tvTitle.text = "退还保证金"
    }

    var ids = 0

    @SuppressLint("SetTextI18n")
    override fun initData() {
        vm.getData()
        binding.apply {
            tvCommit.clickWithTrigger {
                var remark = tvBzj.text.toString()
                if (remark.isNullOrBlank()) {
                    toast("退款原因不能为空")
                    return@clickWithTrigger
                }
                vm.getData2("remark" to remark, "id" to ids)
                //
            }
            tvName.clickWithTrigger {
                if (list.size == 0) {
                    toast("没有可退款的项目")
                    return@clickWithTrigger
                }
                XPopup.Builder(this@BackBondActivity2)
                    .asCustom(BjDialog(this@BackBondActivity2, list) {
                        tvName.text = it.cate_name
                        ids = it.id
                        tvPrice.text = "￥${it.money}"
                    })
                    .show()
            }

        }

    }

    var list = arrayListOf<ReturnBean>()
    override fun startObserve() {
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                startPage<BackSuccessActivity>()
                finish()
            }
        }
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                list.clear()
                list.addAll(it!!)
            }
        }
    }

}